
package com.shop.cloud.mall.admin.controller;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.shop.cloud.common.core.util.R;
import com.shop.cloud.common.log.annotation.SysLog;
import com.shop.cloud.common.security.util.SecurityUtils;
import com.shop.cloud.mall.common.entity.UserShop;
import com.shop.cloud.mall.admin.service.UserShopService;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Operation;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.util.HashMap;
import java.util.Map;

/**
 * 店铺用户
 *
 * @author
 * @date 2021-03-29 14:26:20
 */
@Slf4j
@RestController
@AllArgsConstructor
@RequestMapping("/usershop")
@Tag(description = "usershop", name = "店铺用户管理")
public class UserShopController {

    private final UserShopService userShopService;

    /**
     * 分页列表
     * @param page 分页对象
     * @param userShop 店铺用户
     * @return
     */
    @Operation(summary = "分页列表")
    @GetMapping("/page")
    @PreAuthorize("@ato.hasAuthority('mall:usershop:index')")
    public R getPage(Page page, UserShop userShop) {
        return R.ok(userShopService.page1(page, userShop));
    }

    /**
     * 店铺用户查询
     * @param id
     * @return R
     */
    @Operation(summary = "店铺用户查询")
    @GetMapping("/{id}")
    @PreAuthorize("@ato.hasAuthority('mall:usershop:get')")
    public R getById(@PathVariable("id") String id) {
        return R.ok(userShopService.getById(id));
    }

    /**
     * 店铺用户新增
     * @param userShop 店铺用户
     * @return R
     */
    @Operation(summary = "店铺用户新增")
    @SysLog("新增店铺用户")
    @PostMapping
    @PreAuthorize("@ato.hasAuthority('mall:usershop:add')")
    public R save(@RequestBody UserShop userShop) {
        return R.ok(userShopService.save(userShop));
    }

    /**
     * 店铺用户修改
     * @param userShop 店铺用户
     * @return R
     */
    @Operation(summary = "店铺用户修改")
    @SysLog("修改店铺用户")
    @PutMapping
    @PreAuthorize("@ato.hasAuthority('mall:usershop:edit')")
    public R updateById(@RequestBody UserShop userShop) {
        return R.ok(userShopService.updateById(userShop));
    }

    /**
     * 店铺用户删除
     * @param id
     * @return R
     */
    @Operation(summary = "店铺用户删除")
    @SysLog("删除店铺用户")
    @DeleteMapping("/{id}")
    @PreAuthorize("@ato.hasAuthority('mall:usershop:del')")
    public R removeById(@PathVariable String id) {
        return R.ok(userShopService.removeById(id));
    }

	/**
	 * 查询数量
	 * @param userShop
	 * @return
	 */
	@Operation(summary = "查询数量")
	@GetMapping("/count")
	public R getCount(UserShop userShop) {
		return R.ok(userShopService.count(Wrappers.query(userShop)));
	}

	/**
	 * 查询统计
	 * @param userShop
	 * @return
	 */
	@Operation(summary = "查询统计")
	@GetMapping("/statistics")
	public R getStatistics(UserShop userShop) {
        if(StrUtil.isNotBlank(SecurityUtils.getUser().getShopId())){
            userShop.setShopId(SecurityUtils.getUser().getShopId());
        }
		//总数量
		long countTotal = userShopService.count(Wrappers.query(userShop));
		//今天数量
		LocalDate localDate = LocalDateTime.now().toLocalDate();
		long countToday = userShopService.count(Wrappers.query(userShop).between("create_time", LocalDateTime.of(localDate, LocalTime.MIN), LocalDateTime.of(localDate, LocalTime.MAX)));
		Map<String, Object> rs = new HashMap<>();
		rs.put("countTotal",countTotal);
		rs.put("countToday",countToday);
		return R.ok(rs);
	}
}
